#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.shortcuts import render
from admincms.models import SchoolOrganizationInfo, HomeFeedbackInfo, HomeQuestionnaireInfo, get_content_preview
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect


# Create your views here.
def page_list(request, menu_type):
    menus = get_menus(menu_type)
    if menu_type == 'HomeFeedbackInfo':
        items = get_home_questionnaire_list('HomeFeedbackInfo')
    elif menu_type == 'HomeQuestionnaireInfo':
        items = get_home_questionnaire_list('HomeQuestionnaireInfo')
    else:
        items = get_list(menu_type)
    body = {
        'menu': menus,
        'items': items
    }
    return render(request, "list.html", body)


def page_detail(request, menu_type, article_id):
    menus = get_menus(menu_type)
    detail = get_detail(article_id)
    body = {
        'menu': menus,
        'detail': detail
    }
    return render(request, "detail.html", body)


def search_detail(request, article_id):
    detail = get_detail(article_id)
    body = {
        'detail': detail
    }
    return render(request, "search_detail.html", body)


def feedback(request, menu_type):
    menus = get_menus(menu_type)
    body = {
        'menu': menus,
    }
    return render(request, "feedback.html", body)


@csrf_exempt
def submit_feedback(request, menu_type):
    article_content = request.POST.get('feedback')
    if len(article_content) > 10:
        article_preview = get_content_preview(article_content)
        HomeFeedbackInfo.objects.create(article_title='[访客]意见反馈', article_type='意见反馈', article_preview=article_preview,
                                        article_content=article_content)
    menus = get_menus(menu_type)
    body = {
        'menu': menus,
    }
    return render(request, "feedback.html", body)


def questionnaire_detail(request, article_id):
    menus = get_menus('HomeQuestionnaireInfo')
    detail = get_detail(article_id)
    body = {
        'menu': menus,
        'detail': detail,
    }
    return render(request, "questionnaire_detail.html", body)


@csrf_exempt
def submit_questionnaire(request, article_id):
    questionnaire_html = request.POST.get('questionnaire_html')
    article_preview = get_content_preview(questionnaire_html)
    detail = get_detail(article_id)
    article_title = '[访客]' + detail.article_title
    HomeQuestionnaireInfo.objects.create(article_title=article_title, article_type='问卷调查',
                                         article_preview=article_preview, article_content=questionnaire_html)
    return HttpResponseRedirect('/list/HomeQuestionnaireInfo')


def get_menus(menu_type):
    menus = {
        'parent': None,
        'current': {
            'name': None,
            'url': None,
            'category': None,
        },
        'menus': [{'name': None, 'url': None}],
    }
    current_menu_name = get_menu_name(menu_type)
    menus['current'] = {'name': current_menu_name, 'url': 'http://127.0.0.1:8000/list/' + menu_type, 'category': menu_type}
    if menu_type == 'SchoolOrganizationInfo' or menu_type == 'SchoolHonorInfo' or menu_type == 'SchoolContactInfo':
        menus['parent'] = {'name': '学校概况', 'url': 'http://127.0.0.1:8000/list/SchoolOrganizationInfo'}
        menus['menus'] = [{'name': '组织机构', 'url': 'http://127.0.0.1:8000/list/SchoolOrganizationInfo'},
                          {'name': '学校荣誉', 'url': 'http://127.0.0.1:8000/list/SchoolHonorInfo'},
                          {'name': '联系我们', 'url': 'http://127.0.0.1:8000/list/SchoolContactInfo'},
                          ]
    elif menu_type == 'PartyStaffInfo' or menu_type == 'PartyTeacherInfo':
        menus['parent'] = {'name': '党建工作', 'url': 'http://127.0.0.1:8000/list/PartyStaffInfo'}
        menus['menus'] = [{'name': '教职工党建', 'url': 'http://127.0.0.1:8000/list/PartyStaffInfo'},
                          {'name': '师生党建', 'url': 'http://127.0.0.1:8000/list/PartyTeacherInfo'},
                          ]
    elif menu_type == 'NewsCenterInfo' or menu_type == 'AnnounceCenterInfo' or menu_type == 'MenuCenterInfo':
        menus['parent'] = {'name': '公告中心', 'url': 'http://127.0.0.1:8000/list/NewsCenterInfo'}
        menus['menus'] = [{'name': '校内新闻', 'url': 'http://127.0.0.1:8000/list/NewsCenterInfo'},
                          {'name': '学校公示', 'url': 'http://127.0.0.1:8000/list/AnnounceCenterInfo'},
                          {'name': '每日菜谱', 'url': 'http://127.0.0.1:8000/list/MenuCenterInfo'},
                          ]
    elif menu_type == 'BeautifulBlackboardInfo' or menu_type == 'BeautifulSportInfo' \
            or menu_type == 'BeautifulActivityInfo':
        menus['parent'] = {'name': '精彩校园', 'url': 'http://127.0.0.1:8000/list/BeautifulBlackboardInfo'}
        menus['menus'] = [{'name': '黑板报', 'url': 'http://127.0.0.1:8000/list/BeautifulBlackboardInfo'},
                          {'name': '运动会', 'url': 'http://127.0.0.1:8000/list/BeautifulSportInfo'},
                          {'name': '集体活动', 'url': 'http://127.0.0.1:8000/list/BeautifulActivityInfo'},
                          ]
    elif menu_type == 'HomeQuestionnaireInfo' or menu_type == 'HomeFeedbackInfo':
        menus['parent'] = {'name': '家校互动', 'url': 'http://127.0.0.1:8000/list/HomeQuestionnaireInfo'}
        menus['menus'] = [{'name': '问卷调查', 'url': 'http://127.0.0.1:8000/list/HomeQuestionnaireInfo'},
                          {'name': '意见反馈', 'url': 'http://127.0.0.1:8000/feedback/HomeFeedbackInfo'},
                          ]
    return menus


def get_list(menu_type):
    menu_name = get_menu_name(menu_type)
    articles = SchoolOrganizationInfo.objects.raw('select * from admincms_schoolarticle '
                                                  'where article_type=%s order by id desc', [menu_name])
    return articles


def get_home_questionnaire_list(menu_type):
    menu_name = get_menu_name(menu_type)
    sql = f'select * from admincms_schoolarticle ' \
          f'where article_type=\'{menu_name}\' and article_title not like \'[访客%\' order by id desc limit 15'
    articles = HomeQuestionnaireInfo.objects.raw(sql)
    return articles


def get_detail(article_id):
    article = SchoolOrganizationInfo.objects.raw('select * from admincms_schoolarticle '
                                                 'where id=%s', [article_id])
    return article[0]


def get_menu_name(menu_type):
    if menu_type == 'SchoolOrganizationInfo':
        return '组织机构'
    elif menu_type == 'SchoolHonorInfo':
        return '学校荣誉'
    elif menu_type == 'SchoolContactInfo':
        return '联系我们'
    elif menu_type == 'PartyStaffInfo':
        return '教职工党建'
    elif menu_type == 'PartyTeacherInfo':
        return '师生党建'
    elif menu_type == 'NewsCenterInfo':
        return '校内新闻'
    elif menu_type == 'AnnounceCenterInfo':
        return '学校公示'
    elif menu_type == 'MenuCenterInfo':
        return '每日菜谱'
    elif menu_type == 'BeautifulBlackboardInfo':
        return '黑板报'
    elif menu_type == 'BeautifulSportInfo':
        return '运动会'
    elif menu_type == 'BeautifulActivityInfo':
        return '集体活动'
    elif menu_type == 'HomeQuestionnaireInfo':
        return '问卷调查'
    elif menu_type == 'HomeFeedbackInfo':
        return '意见反馈'
    else:
        return ''

